package com.share.main;

import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.*;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.util.Date;

/**
 * Created By caifan on 2018/6/14
 * 参考：https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.3/java-rest-high-document-get.html#java-rest-high-document-get-request
 */
//@Slf4j
@Log4j2
public class IndexApi {
    private TransportClient client ;
    @Before
    public void getClient() throws Exception {
        /*
         * 单节点
         */
//        client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        /*
         * 集群
         */
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("node.name", "node01").put("client.transport.sniff",true).build();
        client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(""), 9300));
    }


    @Test
    public void mapIndex() throws Exception {
        Settings settings = Settings.builder().build();
        //GetResponse getResponse = client.prepareGet("megacorp", "employee", "2").get();
       // System.out.println(getResponse);
//        System.out.println(client.prepareIndex("megacorp", "employee", "2").get());
        /*IndexRequest request = new IndexRequest(
                "posts",
                "doc",
                "1");
        String jsonString = "{" +
                "\"user\":\"kimchyn\"," +
                "\"postDate\":\"2018-06-30\"," +
                "\"message\":\"trying  Elasticsearch\"" +
                "}";
        request.source(jsonString, XContentType.JSON);
        client.index(request);*/
        /*Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("user", "kimchy");
        jsonMap.put("postDate", new Date());
        jsonMap.put("message", "trying out Elasticsearch");
        IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
                .source(jsonMap);*/
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.field("user", "share");
            builder.timeField("postDate", new Date());
            builder.field("message", " Elasticsearch indexAPI");
            builder.field("age", 26);
            builder.array("name",new String[]{"zhangsan", "lisi"});
        }
        builder.endObject();
        IndexRequest indexRequest = new IndexRequest("posts", "doc", "8")
                .source(builder);
        ActionFuture<IndexResponse> actionFuture = client.index(indexRequest);
        System.out.println(actionFuture.actionGet());
    }

    @Test
    public void getIndexTest() throws Exception {
        log.info("get index test");
        GetRequest getRequest = new GetRequest("index1", "type1", "1");
        ActionFuture<GetResponse> getResponseActionFuture = client.get(getRequest);
        log.info("getResponse:{}", getResponseActionFuture.get());
    }

    @Test
    public void deleteAPI() throws Exception {
        log.info("delete index test");
        //TransportClient client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        DeleteRequest deleteRequest = new DeleteRequest("megacorp", "employee", "3");
        ActionFuture<DeleteResponse> delete = client.delete(deleteRequest);
        log.info("delete:{}", delete.get());
    }

    @Test
    public void updateAPI() throws Exception {
        log.info("update index test");
        UpdateRequest updateRequest = new UpdateRequest("posts", "doc1", "9");
        updateRequest.doc("postDate", new Date(), "phone", "13688886666");
        client.update(updateRequest);
        log.info("updateRequest:{}", updateRequest);
    }

    /**
     * 批量处理index
     * @throws Exception
     * es6中一个索引只能一个type es5中一个索引对应多个type es7将移除type
     */
    @Test
    public void bulkRequestTest() throws Exception {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new IndexRequest("test11", "type1", "1").source(XContentType.JSON, "field", "foo", "user", "share", "age", 23));
        bulkRequest.add(new IndexRequest("test12", "type2", "2").source(XContentType.JSON, "field", "bar", "user", "happy", "age", 28));
        bulkRequest.add(new IndexRequest("test13", "type3", "3").source(XContentType.JSON, "field", "baz", "user", "kimchy", "age", 22));
        ActionFuture<BulkResponse> bulk = client.bulk(bulkRequest);
        log.info("bulkRequest:{}", bulk.actionGet());
    }

    @Test
    public void multiGetTest() throws Exception {
        MultiGetRequest request = new MultiGetRequest();
        request.add(new MultiGetRequest.Item(
                "index2",
                "type2",
                "2"));
        request.add(new MultiGetRequest.Item("posts", "doc", "8"));
        log.info("multiGet:{}", client.multiGet(request).actionGet());
        for (MultiGetItemResponse responses : client.multiGet(request).get().getResponses()) {
            System.out.println(responses.getResponse());
        }
    }

}
